package com.example.questionnaire.util;


import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CommonUtil {

    // 创建单元格
    public static Cell createCell(String content) {
        Cell cell = new Cell();
        Paragraph paragraph = new Paragraph(content);
        cell.add(paragraph);
        cell.setTextAlignment(TextAlignment.CENTER);
        return cell;
    }



    public static void ImageUpload(MultipartFile multipartFile, String formid, String ImageStored, String filename, String ImageName){
        try {
//获取文件字节数组
            byte [] bytes = multipartFile.getBytes();
//文件存储路径(/fileupload1/ 这样会在根目录下创建问价夹)
            File pfile = new File( ImageStored + filename + formid);

            System.out.println(pfile);
//判断文件夹是否存在
            if(!pfile.exists()){
//不存在时,创建文件夹
                pfile.mkdirs();

            }
//创建文件
            File file = new File(pfile, ImageName+".jpg");
//写入指定文件夹
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }



    public static boolean CompareTheCurrentDates(Date CompareTime){
        SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = String.valueOf(CompareTime);
        try {
            Date date = sim1.parse(s1);
            Date date1 = new Date();
            return sim2.format(date1).equals(sim2.format(date));
        }catch (ParseException e ){
            e.printStackTrace();
        }
        return false;
    }



    /**
     * 卡号生成规则
     * 编号：是否为测试+7位随机数
     * @return
     */
    public static StringBuilder getRandomId() {
        StringBuilder formId = new StringBuilder();
        //cardNumber.append(isTest);
        for (int i = 0; i < 9; i++) {
            int temp = (int) (Math.random() * 10);
            formId.append(temp);
        }

        return formId;
    }

    /**
     * 获取username编号
     * 规则：
     * 编号：年份+七位的百万数字
     * 账号：D+编号
     *
     * @param start 开始编号
     * @param end   结束编号
     * @return
     */
    public static List<String> getUsersNumberingList(Integer start, Integer end) {
        List<String> numberingList = new ArrayList<>();
//        String currentYear = getYear();
        String currentYear = getYear();
        DecimalFormat decimalFormat = new DecimalFormat("0000000");
        for (int i = start; i <= end; i++) {
            String formatUsername = decimalFormat.format(i);
            String numbering = currentYear + formatUsername;
            numberingList.add(numbering);
        }
        return numberingList;
    }

    /**
     * 获取当天日期的年
     */
    public static String getYear() {
        LocalDateTime currentTime = LocalDateTime.now();
        Integer currentYear = currentTime.getYear();
        return currentYear.toString();
    }

    /**
     * 随机生成一个6位数的密码，纯数字
     *
     * @return
     */
    public static String getRandomPassword() {
        char[] randomChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        String randomPassword = String.valueOf(randomChar[random.nextInt(10)]);
        for (int i = 0; i < 5; i++) {
            randomPassword += String.valueOf(randomChar[random.nextInt(10)]);
        }
        return randomPassword;
    }


    /**
     * 生成不重复id
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    /**
     * 手机号正则表达式
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
        String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 邮箱正则表达式
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String check = "^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean isMatched = matcher.matches();
        return isMatched;
    }



}
